import Vue from 'vue'
import { axios } from '@/utils/request'
import qs from 'qs'
import fileDownload from "js-file-download";



// get
export function getRequest(url,params) {
  return axios({
    url: url,
    method:'get' ,
    params
  })
}

export function getRequestUrl(url,params) {
  return axios({
    url: url + '?id=' + params.id + '&newDate=' + new Date().getTime(),
    method:'get' ,
    headers: { 'content-type': 'application/x-www-form-urlencoded' },
  })
}
export function postRequestUrl2(url,data) {
  return axios({
    url: url + `?baseId=${data.baseId}`,
    method:'post' ,
  })
}
export function postRequestUrl(url,data) {
  return axios({
    url: url,
    method:'post' ,
    data  
  })
}



/**
 * 下载文件 用于excel导出
 * @param url
 * @param parameter
 * @returns {*}
 */
export function downFile(url,parameter,method = 'post'){
  return axios({
    url: url,
    data: qs.stringify(parameter),
    method,
    responseType: 'blob',
    headers: { 'content-type': 'application/x-www-form-urlencoded' },
  })
}





/**
 * 文件上传 用于富文本上传图片
 * @param url
 * @param parameter
 * @returns {*}
 */
export function uploadAction(url,parameter){
  return axios({
    url: url,
    data: (parameter),
    method:'post' ,
    headers: {
      'content-type': 'multipart/form-data',  // 文件上传
    },
  })
}

// 用组件
export function downloadFile(url, parameter,method ) {
  return downFile(url, parameter,method).then((data) => {
    if (!data || data.size === 0) {
      Vue.prototype['$message'].warning('文件下载失败')
      return
    }

    var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
      var contentDisposition = decodeURI(data.headers['content-disposition'])
      var result = patt.exec(contentDisposition)


      try {
        var fileName = result[1]
        fileName = fileName.replace(/\"/g, '')
      } catch (error) {
        return Vue.prototype['$message'].warning('文件下载失败')
      }
      if(!fileName) return Vue.prototype['$message'].warning('文件下载失败')

    fileDownload(data.data,fileName)
  })
}
export function downloadFile2(url, parameter,method ) {
  return downFilePost(url, parameter,method).then((data) => {
    if (!data || data.size === 0) {
      Vue.prototype['$message'].warning('文件下载失败')
      return
    }

    var patt = new RegExp('filename=([^;]+\\.[^\\.;]+);*')
      var contentDisposition = decodeURI(data.headers['content-disposition'])
      var result = patt.exec(contentDisposition)
      try {
        var fileName = result[1]
        fileName = fileName.replace(/\"/g, '')
      } catch (error) {
      }

      console.log(data);
      if(!fileName) return Vue.prototype['$message'].warning('文件下载失败')
    fileDownload(data.data,fileName)
  })
}



/**
 * 下载文件 用于excel导出
 * @param url
 * @param parameter
 * @returns {*}
 */
export function downFilePost(url,parameter,method = 'post'){
  return axios({
    url: url,
    data: parameter,
    method,
    responseType: 'blob',

  })
}























const api = {
  user: '/mock/api/user',
  role: '/mock/api/role',
  service: '/mock/api/service',
  permission: '/mock/api/permission',
  permissionNoPager: '/mock/api/permission/no-pager',


}

export default api

export function postTest(url,parameter) {
  return axios({
    url: url,
    method:'post' ,
    data: parameter,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  })
}


export function postAction(url,data) {
  let obj = {
    url: url,
    method:'post' ,
  }
  if(data){
    obj.data = data
  }
  return axios(obj)
}

export function httpAction(url,parameter,method) {
  return axios({
    url: url,
    method:method ,
    data: parameter
  })
}

//put
export function putAction(url,parameter) {
  return axios({
    url: url,
    method:'put',
    data: parameter
  })
}

//get
export function getAction(url,parameter) {
  return axios({
    url: url,
    method: 'get',
    params: parameter,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
  })
}

//deleteAction
export function deleteAction(url,parameter) {
  return axios({
    url: url,
    method: 'delete',
    params: parameter
  })
}

export function getUserList(parameter) {
  return axios({
    url: api.user,
    method: 'get',
    params: parameter
  })
}

export function getRoleList(parameter) {
  return axios({
    url: api.role,
    method: 'get',
    params: parameter
  })
}

export function getServiceList(parameter) {
  return axios({
    url: api.service,
    method: 'get',
    params: parameter
  })
}

export function getPermissions(parameter) {
  return axios({
    url: api.permissionNoPager,
    method: 'get',
    params: parameter
  })
}

// id == 0 add     post
// id != 0 update  put
export function saveService(parameter) {
  return axios({
    url: api.service,
    method: parameter.id == 0 ? 'post' : 'put',
    data: parameter
  })
}


//post 下载文件 excel导出
export function downFiles(url,data){
  return axios({
    url: url,
    data,
    method:'post' ,
    responseType: 'blob'
  })
}




/**
 * 获取文件服务访问路径
 * @param avatar
 * @param subStr
 * @returns {*}
 */
export function getFileAccessHttpUrl(avatar,subStr) {
  if(!subStr) subStr = 'http'
  try {
    if(avatar && avatar.startsWith(subStr)){
      return avatar;
    }else{
      if(avatar && avatar.length>0 && avatar.indexOf('[')==-1){
        return window._CONFIG['staticDomainURL'] + "/" + avatar;
      }
    }
  }catch(err){
   return;
  }
}
